/** @jsxImportSource @emotion/react */
import * as React from "react";
import {
    Text, LineColumn, InputLine, SuffixInput, Table, TableBody, TableRow, CCell, BlobInputList, Input,
} from "customize-easy-ui-component";
import {InspectRecordLayout, InternalItemProps, RepLink, SelectHookfork, useItemInputControl,} from "../../common/base";
import {useMeasureInpFilter} from "../../common/hooks";
import {arraySetInp, calcAverageArrObj} from "../../../common/tool";
import {resTranslJd} from "../../common/helper";


interface SpeedLimiterItemProps  extends InternalItemProps{
    usecj?: number;    //2=对重限速器？   默认usecj==1=轿厢限速器；
}
export const itemA动速校: string[] = [ ];
['对','轿'].forEach(type => {
    const itemnms=['限制单','限额速','钳匹类','气速下','械速下','气速上','械速上','度下限','度上限','速器论'].map(a=>a+type);
    itemA动速校.push(...itemnms);
});
export const SpeedLimiter =
    React.forwardRef(({children, show, alone = true, refWidth, label,usecj}: SpeedLimiterItemProps, ref
    ) => {
        const baseN= usecj===2? '对':'轿';
        const [getInpFilter]=useMeasureInpFilter(null, itemA动速校, );        //hook死循环
        const {inp, setInp} = useItemInputControl({ ref });
        // const aveThick=calcAverageArrObj([inp?.力面厚1o,inp?.力面厚2o,inp?.力面厚3o],(row)=>row,2);
        return (
            <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show}
                                 alone={alone} label={label+`${usecj===2? '对重':'轿厢'}`}>
                <Text variant="h5">{label}：</Text>
                校验--{usecj===2? '对重':'轿厢'}限速器，其基本参数是：<br/>
                <InputLine  label='限速器制造单位' >
                    <BlobInputList  value={inp?.['限制单'+baseN] ||''}
                                    onListChange={v => setInp({...inp,['限制单'+baseN]:v || undefined}) }/>
                </InputLine>
                <LineColumn column={6} >
                    <InputLine  label='额定速度' >
                        <SuffixInput  value={inp?.['限额速'+baseN] ||''} onSave={txt=> setInp({...inp,['限额速'+baseN]: txt || undefined })}>m/s</SuffixInput>
                    </InputLine>
                    <InputLine  label='安全钳匹配类型' >
                        <Input  value={inp?.['钳匹类'+baseN] ||''}
                                onChange={e => setInp({...inp,['钳匹类'+baseN]:e.currentTarget.value || undefined}) }/>
                    </InputLine>
                </LineColumn>
                {usecj===2? '对重':'轿厢'}限速器的观测值： {'>>'}
                <LineColumn column={2} >
                    <div>
                        <Text css={{marginLeft: '3rem'}}>电气动作速度（下行） 测量值</Text>
                        <LineColumn column={8}  >
                            { (new Array(3)).fill(null).map(( _,  w:number) => {
                                return <InputLine key={w} label={`v1${w+1} =`}>
                                    <SuffixInput  value={inp?.['气速下'+baseN]?.[w] ||''} onSave={txt=>arraySetInp('气速下'+baseN,w,txt,inp,setInp)}>m/s</SuffixInput>
                                </InputLine>;
                            }) }
                            <Text>电气动作速度（下行）平均值= {calcAverageArrObj(inp?.['气速下'+baseN],a=>a,2,3)} m/s</Text>
                            {/* const avspeed2=calcAverageArrObj(orc.主降速表,(row)=>{return row?.d/row?.t},2); */}
                        </LineColumn>
                    </div>
                    <div>
                        <Text css={{marginLeft: '3rem'}}>机械动作速度（下行） 测量值</Text>
                        <LineColumn column={8}  >
                            { (new Array(3)).fill(null).map(( _,  w:number) => {
                                return <InputLine key={w} label={`v2${w+1} =`}>
                                    <SuffixInput  value={inp?.['械速下'+baseN]?.[w] ||''} onSave={txt=>arraySetInp('械速下'+baseN,w,txt,inp,setInp)}>m/s</SuffixInput>
                                </InputLine>;
                            }) }
                            <Text>机械动作速度（下行）平均值= {calcAverageArrObj(inp?.['械速下'+baseN],a=>a,2,3)} m/s</Text>
                        </LineColumn>
                    </div>
                    <div>
                        <Text css={{marginLeft: '3rem'}}>电气动作速度（上行） 测量值</Text>
                        <LineColumn column={8}  >
                            { (new Array(3)).fill(null).map(( _,  w:number) => {
                                return <InputLine key={w} label={`v3${w+1} =`}>
                                    <SuffixInput  value={inp?.['气速上'+baseN]?.[w] ||''} onSave={txt=>arraySetInp('气速上'+baseN,w,txt,inp,setInp)}>m/s</SuffixInput>
                                </InputLine>;
                            }) }
                            <Text>电气动作速度（上行）平均值= {calcAverageArrObj(inp?.['气速上'+baseN],a=>a,2,3)} m/s</Text>
                        </LineColumn>
                    </div>
                    <div>
                        <Text css={{marginLeft: '3rem'}}>机械动作速度（上行） 测量值</Text>
                        <LineColumn column={8}  >
                            { (new Array(3)).fill(null).map(( _,  w:number) => {
                                return <InputLine key={w} label={`v4${w+1} =`}>
                                    <SuffixInput  value={inp?.['械速上'+baseN]?.[w] ||''} onSave={txt=>arraySetInp('械速上'+baseN,w,txt,inp,setInp)}>m/s</SuffixInput>
                                </InputLine>;
                            }) }
                            <Text>机械动作速度（上行）平均值= {calcAverageArrObj(inp?.['械速上'+baseN],a=>a,2,3)} m/s</Text>
                        </LineColumn>
                    </div>
                </LineColumn>
                <LineColumn column={6} >
                    <InputLine  label='动作速度下限值' >
                        <SuffixInput  value={inp?.['度下限'+baseN] ||''} onSave={txt=> setInp({...inp,['度下限'+baseN]: txt || undefined })}>m/s</SuffixInput>
                    </InputLine>
                    <InputLine  label='动作速度上限值' >
                        <SuffixInput  value={inp?.['度上限'+baseN] ||''} onSave={txt=> setInp({...inp,['度上限'+baseN]: txt || undefined })}>m/s</SuffixInput>
                    </InputLine>
                    <InputLine label={`${usecj===2? '对重':'轿厢'}限速器-校验结论`}>
                        <SelectHookfork value={inp?.['速器论'+baseN] ||''}
                                        onChange={e => setInp({ ...inp, ['速器论'+baseN]: e.currentTarget.value||undefined}) }/>
                    </InputLine>
                </LineColumn>
            </InspectRecordLayout>
        );
});

const 限速器动作速度校验= ({ orc, rep, usecj } : { orc: any, rep: any, usecj?:number}
) => {
    const utag=usecj===2? 'DzLimiter':'Limiter';    //两种分开编辑器
    const baseN= usecj===2? '对':'轿';
    return <>
                <TableRow id={usecj===2? 'DzLimiter' : 'Limiter'}>
                    <CCell colSpan={8}>{usecj===2? '对重': '轿厢'}限速器</CCell>
                </TableRow>
                <TableRow>
                    <CCell>限速器制造单位</CCell>
                    <CCell colSpan={3}>{orc?.['限制单'+baseN] || '/'}</CCell>
                    <CCell>出厂编号</CCell>
                    <CCell colSpan={3}>{orc?.[ (usecj===2? '对限速':'限速器')+'编号'] || '/'}</CCell>
                </TableRow>
                <TableRow>
                    <CCell>限速器型号</CCell>
                    <CCell colSpan={3}>{orc?.[ (usecj===2? '对限速':'限速器')+'型号'] || '/'}</CCell>
                    <CCell>额定速度</CCell>
                    <CCell colSpan={2}>{orc?.['限额速'+baseN] || '/'}</CCell>
                    <CCell>m/s</CCell>
                </TableRow>
                <TableRow>
                    <CCell>安全钳匹配类型</CCell>
                    <CCell colSpan={7}>{orc?.['钳匹类'+baseN] || '/'}</CCell>
                </TableRow>
                <TableRow>
                    <CCell colSpan={8}>观测值</CCell>
                </TableRow>
                { [['下',[1,2]],['上',[3,4]]].map(([flt,bq], i:number) => {
                    return  <React.Fragment key={i}>
                        <RepLink ori rep={rep} tag={usecj===2? 'DzLimiter' : 'Limiter'}>
                            <TableRow>
                                <CCell rowSpan={3}>电气动作速度（{flt}行）测量值</CCell>
                                <CCell>v{bq[0]}1 =</CCell>
                                <CCell>{orc?.['气速'+flt+baseN]?.[0] || '/'}</CCell>
                                <CCell>m/s</CCell>
                                <CCell rowSpan={3}>机械动作速度（{flt}行）测量值</CCell>
                                <CCell>v{bq[1]}1 =</CCell>
                                <CCell>{orc?.['械速'+flt+baseN]?.[0] || '/'}</CCell>
                                <CCell>m/s</CCell>
                            </TableRow>
                            <TableRow>
                                <CCell>v{bq[0]}2 =</CCell>
                                <CCell>{orc?.['气速'+flt+baseN]?.[1] || '/'}</CCell>
                                <CCell>m/s</CCell>
                                <CCell>v{bq[1]}2 =</CCell>
                                <CCell>{orc?.['械速'+flt+baseN]?.[1] || '/'}</CCell>
                                <CCell>m/s</CCell>
                            </TableRow>
                            <TableRow>
                                <CCell>v{bq[0]}3 =</CCell>
                                <CCell>{orc?.['气速'+flt+baseN]?.[2] || '/'}</CCell>
                                <CCell>m/s</CCell>
                                <CCell>v{bq[1]}3 =</CCell>
                                <CCell>{orc?.['械速'+flt+baseN]?.[2] || '/'}</CCell>
                                <CCell>m/s</CCell>
                            </TableRow>
                            <TableRow>
                                <CCell>电气动作速度（{flt}行）平均值</CCell>
                                <CCell colSpan={2}>{calcAverageArrObj(orc?.['气速'+flt+baseN],a=>a,2,3)}</CCell>
                                <CCell>m/s</CCell>
                                <CCell>机械动作速度（{flt}行）平均值</CCell>
                                <CCell colSpan={2}>{calcAverageArrObj(orc?.['械速'+flt+baseN],a=>a,2,3)}</CCell>
                                <CCell>m/s</CCell>
                            </TableRow>
                        </RepLink>
                    </React.Fragment>;
                }) }
                <TableRow>
                    <CCell>动作速度下限值</CCell>
                    <CCell colSpan={2}>{orc?.['度下限'+baseN] || '/'}</CCell>
                    <CCell>m/s</CCell>
                    <CCell>动作速度上限值</CCell>
                    <CCell colSpan={2}>{orc?.['度上限'+baseN] || '/'}</CCell>
                    <CCell>m/s</CCell>
                </TableRow>
                <TableRow>
                    <CCell>校验结论</CCell>
                    <CCell colSpan={7}>{resTranslJd(orc?.['速器论'+baseN]) || '/'}</CCell>
                </TableRow>
    </>;
};

export const SpeedLimiterVw= ({children, orc, rep,label } : { orc: any, rep: any,label:any, children?: any}
) => {
    return <>
        <div css={{"@media print": {paddingBottom: '7rem', pageBreakInside: 'avoid'}}}>
            <Text variant="h4" css={{marginTop: '1rem',
            }}>{label}</Text>
        </div>
        <Table fixed={ ["20%","8%","15%","8%","%","8%","12%","8%"] }
                                   css={ {borderCollapse: 'collapse', "@media print": {marginTop: '-7rem'}} }  tight  miniw={800}>
            <TableBody>
                <RepLink ori rep={rep} tag={'Limiter'}>
                    {限速器动作速度校验({orc, rep})}
                </RepLink>
                <RepLink ori rep={rep} tag={'DzLimiter'}>
                    {限速器动作速度校验({orc, rep, usecj:2})}
                </RepLink>
            </TableBody>
        </Table>
    </>;
};
